Ccay.Module.define("ccay.security.user.edit",function(page,$S){
	var userId = "";
	// 初始化方法
	page.init = function(initUserId){
		userId = initUserId;
		$S("#userId").val(initUserId);
		//是修改需要初始化信息
		if(userId){
			Ccay.doGet({
	 			async:false,
	 			url:"services/manage/security/userManage/findUserById/"+userId,
	 			success:function(userInfo){
	 				$S("#userForm").jsonData(userInfo);
	 			}			
	 		});
		}
	}
	page.save = function(){
		if(!$S("#userForm").valid()){
			return;
		}
		var url = userId ? "services/manage/security/userManage/update" : "services/manage/security/userManage/create";
		var postData = $S("#userForm").jsonData();
		postData.enabled = !!$("#enabled:checked").length;
		Ccay.doPost({
 			async:false,
 			url:url,
 			data:postData,
 			success:function(info){
 				if(info.userNameDuplicate){
 					Ccay.UI.topBox(info.userNameDuplicate);
 				}else if(info.userDisplayNameDuplicate){
 					Ccay.UI.topBox(info.userDisplayNameDuplicate);
 				}else{
 					ccay.security.user.list.research();
 					page.closeDialog();
 				}
 			}			
 		});
	}
	
	$.validator.addMethod("checkUsernameUnique", function(value, element, param) {
		var result = true;
		param = {userName:value};
		if(userId){
			param.userId = userId;
		}
		Ccay.doGet({
 			async:false,
 			url:"services/manage/security/userManage/checkUsernameUnique",
 			data :param,
 			success:function(data){
 				result = data;
 			}			
 		});
 		return result;
	}, $i18n("ccay.usermanage.warn.usernameExist"));
	
	$.validator.addMethod("checkUserDisplayNameUnique", function(value, element, param) {
		var result = true;
		param = {userName:value};
		if(userId){
			param.userId = userId;
		}
		Ccay.doGet({
 			async:false,
 			url:"services/manage/security/userManage/checkUserDisplayNameUnique",
 			data :param,
 			success:function(data){
 				result = data;
 			}			
 		});
 		return result;
	}, $i18n("ccay.usermanage.warn.userDisplaynameExist"));
	
	page.close = function(){
		page.closeDialog();
	}
});